package com.eax.CLILayer;
import java.util.Scanner;
import com.eax.ControlLayer.*;

public class MainMenuUI{

	public static void main(String[] args) {
		AddressController addressController = new AddressController();
		DVDController dvdController = new DVDController();
		LendController lendController = new LendController();
		ReservationController reservationController = new ReservationController();

		// Add some data so we can test.
		addressController.createPerson(1, "Eax", "Nope", 9000, "EaxTown", 30827331);
		addressController.createPerson(2, "Linda", "Skyvej", 9400, "Aalborg", 12345678);

		dvdController.createDVD(1, "Die Hard", "Bruce Willis", "15 July 1988");
		dvdController.createDVD(2, "Four weddings and a funeral", "Hugh Grant", "9 March 1994");

		dvdController.createCopy(1, 1, "24/01/1991", 256, "available", true);
		dvdController.createCopy(2, 2, "02/11/1992", 128, "available", true);

		lendController.addLend(1, 1, 1, "24/01/1991", "27/01/1991", "24/01/1991", "26/01/1991", "returned");
		lendController.addLend(2, 2, 1, "24/01/2013", "26/01/2013", "24/01/2013", "26/01/2013", "returned");

		reservationController.addReservation(1, 1, 1, "waiting", "24/01/2014");
		reservationController.addReservation(2, 1, 1, "waiting", "25/01/2014");

		MainMenuUI mainMenu = new MainMenuUI();
		mainMenu.mainMenuStart(addressController, dvdController, lendController, reservationController);
	}

	public void mainMenuStart(AddressController addressController, DVDController dvdController, LendController lendController, ReservationController reservationController){
		boolean exit = false;

		while (!exit) {
			int choice = writeMainMenu();

			switch (choice) {
				case 1:
					startAddressBook(addressController, dvdController, lendController, reservationController);
					break;
				case 2:
					startDvd(dvdController);
					break;
				case 3:
					startLend(lendController, dvdController, addressController, reservationController);
					break;
				case 4:
					startReservation(reservationController, dvdController, addressController);
				default:
					writeEnd();
					exit = true;
					break;
			}
		}
	}

	private int writeMainMenu() {
		// creates a keyboard object to read input
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\f *** MainMenu ***");
		System.out.println(" (1) AddressBook");
		System.out.println(" (2) DVDs");
		System.out.println(" (3) Loan");
		System.out.println(" (4) Reservations");
		System.out.println(" (5) Quit");
		System.out.print("\n Make your choice: ");

		return keyboard.nextInt();
	}

	private void startAddressBook(AddressController addressController, DVDController dvdController, LendController lendController, ReservationController reservationController) {
		AddressBookUI addressBookUi;
		addressBookUi = new AddressBookUI();
		addressBookUi.start(addressController, dvdController, lendController, reservationController);
	}

	private void startDvd(DVDController dvdController) {
		DVDUI DVDui;
		DVDui = new DVDUI(dvdController);
		DVDui.DVDMenu();
	}

	private void startLend(LendController lendController, DVDController dvdController, AddressController addressController, ReservationController reservationController){
		LendUI lendUi;
		lendUi = new LendUI(lendController, dvdController, addressController, reservationController);
		lendUi.LendMenu();
	}

	private void startReservation(ReservationController reservationController, DVDController dvdController, AddressController addressController){
		ReservationUI reservationUi;
		reservationUi = new ReservationUI(reservationController, dvdController, addressController);
		reservationUi.ReservationMenu();
	}

	private void writeEnd() {
		System.out.println(" The System is shutting down ");
	}
}
